package com.nl.lib_core.base

import android.os.Bundle
import android.view.KeyEvent
import com.alibaba.android.arouter.launcher.ARouter
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ktx.immersionBar
import com.nl.lib_core.event.core.BaseActivityEvent
import com.nl.lib_core.mvp.BaseModel
import com.nl.lib_core.mvp.BasePresenter
import com.nl.lib_core.tool.ToastUtil
import org.greenrobot.eventbus.EventBus

/** 作者: wangfei 创建时间: 2021/9/1 包名: com.nl.lib_core.base 类名: BaseMvpActivity */
abstract class BaseMvpActivity<M : BaseModel, V, P : BasePresenter<M, V>> : BaseActivity() {

  protected val mPresenter: P by lazy { initPresenter() }

  override fun onCreate(savedInstanceState: Bundle?) {
    ARouter.getInstance().inject(this)
    EventBus.getDefault().register(this)
    mPresenter.attach(this as V)
    mPresenter.injectLifecycleOwner(this)
    super.onCreate(savedInstanceState)
  }

  override fun initImmersionBar() {
    immersionBar { hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR) }
  }

  override fun onStop() {
    super.onStop()
    ToastUtil.cancel()
  }

  override fun onDestroy() {
    super.onDestroy()
    this.immersionBar {}
    mPresenter.detach()
  }

  abstract fun initPresenter(): P

  override fun <T> onEvent(event: BaseActivityEvent<T>) {}

  override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
    //    if (keyCode == KEYCODE_BACK && event?.action == ACTION_UP){
    //      return false
    //    }
    return super.onKeyUp(keyCode, event)
  }
}
